package com.coolgor.axnut.service.mdmis.impl;

import java.util.ArrayList;
import java.util.List;

//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coolgor.axnut.dao.mdmis.PolicyMonitorDao;
import com.coolgor.axnut.dto.mdmis.PolicyMonitorExecution;
import com.coolgor.axnut.entity.mdmis.PolicyMonitor;
import com.coolgor.axnut.enums.mdmis.PolicyMonitorOperationStateEnum;
import com.coolgor.axnut.exceptions.mdmis.PolicyMonitorOperationException;
import com.coolgor.axnut.service.mdmis.PolicyMonitorService;
import com.coolgor.axnut.util.PageCalculator;

@Service
public class PolicyMonitorServiceImpl implements PolicyMonitorService {
	
	@Autowired
	private PolicyMonitorDao policyMonitorDao;


	@Override
	public PolicyMonitor getPolicyMonitor(String insureNo) {
		return policyMonitorDao.queryPolicyMonitorByInsuerNo(insureNo);
	}

	@Override
	public PolicyMonitorExecution getPolicyMonitorList(PolicyMonitor policyMonitorCondition, int pageIndex, int pageSize) {
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//依据查询条件，调用dao层返回相关的查询结果列表
		List<PolicyMonitor> list = policyMonitorDao.queryPolicyMonitorList(policyMonitorCondition,rowIndex,pageSize);
		//依据相同的查询条件，返回查询结果总数
		int count = policyMonitorDao.queryPolicyMonitorCount(policyMonitorCondition);
		PolicyMonitorExecution result = new PolicyMonitorExecution();
		if (list != null) {
			result.setState(PolicyMonitorOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(PolicyMonitorOperationStateEnum.SUCCESS.getStateInfo());
			result.setList(list);
			result.setCount(count);
		} else {
			result.setState(PolicyMonitorOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	@Transactional
	public PolicyMonitorExecution addPolicyMonitor(PolicyMonitor policyMonitor) {
		// 空值判断
		if (policyMonitor == null) {
			return new PolicyMonitorExecution(PolicyMonitorOperationStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 添加
			effectedNum = policyMonitorDao.insertPolicyMonitor(policyMonitor);
			if (effectedNum <= 0) {
				throw new PolicyMonitorOperationException("保单中断信息创建失败");
			} 
		} catch (Exception e) {
			throw new PolicyMonitorOperationException("add policyMonitor Information error:" + e.getMessage());
		}
		return new PolicyMonitorExecution(PolicyMonitorOperationStateEnum.SUCCESS, policyMonitor);
	}

	@Override
	@Transactional
	public PolicyMonitorExecution modifyPolicyMonitor(PolicyMonitor policyMonitor) {
		// 空值判断
		if (policyMonitor == null) {
			return new PolicyMonitorExecution(PolicyMonitorOperationStateEnum.NULL);
		}
		int effectedNum;
		// 即将更新信息
		try {
			// 更新操作
			effectedNum = policyMonitorDao.updatePolicyMonitor(policyMonitor);
			if (effectedNum <= 0) {
				throw new PolicyMonitorOperationException("保单中断信息更新失败");
			} 
		} catch (Exception e) {
			throw new PolicyMonitorOperationException("Modify policy Monitor Information error:" + e.getMessage());
		}
		return new PolicyMonitorExecution(PolicyMonitorOperationStateEnum.SUCCESS, policyMonitor);
	}

	@Override
	@Transactional
	public PolicyMonitorExecution removePolicyMonitor(PolicyMonitor policyMonitor) {
		// 空值判断
		if (policyMonitor == null) {
			return new PolicyMonitorExecution(PolicyMonitorOperationStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = policyMonitorDao.deletePolicyMonitor(policyMonitor);
			if (effectedNum <= 0) {
				throw new PolicyMonitorOperationException("保单中断信息删除失败");
			} 
		} catch (Exception e) {
			throw new PolicyMonitorOperationException("Delete policy Monitor Information error:" + e.getMessage());
		}
		return new PolicyMonitorExecution(PolicyMonitorOperationStateEnum.SUCCESS, policyMonitor);
	}

	@Override
	@Transactional
	public PolicyMonitorExecution batchRemovePolicyMonitor(List<PolicyMonitor> list) {
		// 空值判断
		if (list == null || list.size()<1) {
			return new PolicyMonitorExecution(PolicyMonitorOperationStateEnum.NULL);
		}
		List<String> insureNoList = new ArrayList<String>();
		for (int i=0; i<list.size(); i++) {
			insureNoList.add(list.get(i).getInsureNo());			
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = policyMonitorDao.batchDeletePolicyMonitor(insureNoList);
			if (effectedNum <= 0) {
				throw new PolicyMonitorOperationException("保单中断信息批量删除失败");
			} 
		} catch (Exception e) {
			throw new PolicyMonitorOperationException("Batch delete Policy Monitor Information error:" + e.getMessage());
		}
		PolicyMonitorExecution exe = new PolicyMonitorExecution(PolicyMonitorOperationStateEnum.SUCCESS);
		//将删除的记录总数加入到返回值
		exe.setCount(effectedNum);
		return exe;
	}

}
